<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" charset="utf-8">
		var lastIndex;
		$(function() {
		
			var rid = $('#orderReturnForm').find('input[name=rid]');
			var ridCombobox = rid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto',
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});
			var uid = $('#orderReturnForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto',
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});
					
			var date = new Date();
			$("#orderDate").val(date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate());

			$.ajax({
				url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
				data : {sign : 'RKTH'},
				type : "POST",
				success : function(d){
					$('#serial').val(d);
				}
			});

			$('#dg').datagrid({
				idField : 'id',
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
					}
				},'-',{
					text: '删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelected');
						if (row){
							var index = $('#dg').datagrid('getRowIndex', row);
							$('#dg').datagrid('deleteRow', index);
						}
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						
						var items = [];
						var rows = $('#dg').datagrid('getRows');
						for ( var i = 0; i < rows.length; i++) {
							items.push(rows[i].id);
							items.push(rows[i].num);
						}
						$('#items').val(items.join(','));
						
						$('#orderReturnForm').form('submit', {
							url : '${pageContext.request.contextPath}/rkgl/orderReturnAction!add.html',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									//TODO
								}
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
				}
			});
			
			$(".barcode").live("keypress",function() {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					_search();
				}
				
			});
		});


		
		function _search() {
			$('#dg').datagrid('endEdit', lastIndex);
			var rid = $('#orderReturnForm').find('input[name=rid]').val();
			$(".rid").attr("name","rid");
			$(".rid").attr("value",rid);
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_search.html',
				success : function(d) {
					var json = $.parseJSON(d);
					if (json.success==null) {
						$('#dg').datagrid('appendRow',{
							id:json.id,
							type:json.type,
							gtname:json.gtname,
							bname:json.bname,
							name:json.name,
							unit:json.unit,
							color:json.color,
							size:json.size,
							num:1
						});
					}else{
						parent.sy.messagerShow({
							msg : json.msg,
							title : '提示'
						});
					}
				}
			});
			$(".rid").attr("name","");
			lastIndex = $('#dg').datagrid('getRows').length-1;
			$('#dg').datagrid('selectRow', lastIndex);
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
	
		<div data-options="region:'north',border:false" style="height: 140px; overflow: hidden;">
			<form id="orderReturnForm" style="height:100px;">
				<input id="items" name="items" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日期</th>
						<td style="width: 200px;"><input name="date" class="easyui-datebox" id="orderDate" data-options="editable:false" style="width: 200px;" /></td>
						<th style="width: 80px;">单号</th>
						<td style="width: 200px;"><input name="serial" id="serial" style="width: 200px;" value=""/></td>
					</tr>
					<tr>
						<th style="width: 80px;">退货仓库 </th>
						<td style="width: 200px;"><input name="rid" style="width: 200px;" /></td>
						<th style="width: 80px;">操作员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;"/></td>
					</tr>
					<tr>
						<th>备注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 480px;"></textarea></td>
					</tr>
				</table>
			</form>
			
			<form id="searchForm" style="height:10px;margin-top:10px;">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<input class="rid" type="hidden"/>
						<th style="width: 80px;">商品条码</th>
						<td style="width: 200px;"><input class="barcode" name="barcode" style="width: 200px;" /></td>
						<td style="width: 280px;" align="right">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	
		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		        <thead>
		            <tr>
		            	<th data-options="field:'id',width:100,hidden:true">商品ID</th>  
		                <th data-options="field:'type',width:100">款号/款式</th>  
		                <th data-options="field:'gtname',width:100">商品类别</th>
		                <th data-options="field:'bname',width:100">品牌</th>
	                 	<th data-options="field:'name',width:100">商品名称</th>
	                  	<th data-options="field:'unit',width:100">单位</th>
                   		<th data-options="field:'color',width:100">颜色</th>
                   		<th data-options="field:'size',width:100">尺码</th>
		                <th data-options="field:'num',width:100,align:'center',editor:{type:'numberbox'}">退货数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</body>
</html>